<?php
class ModuleBase {
	/* SQL pour module base
	 * select mod_module, mtr_controller, mtr_method from glbo_user as user, glbo_access_right as access, glbo_module as module, glbo_module_tree as tree where user.usr_id=access.usr_id AND access.mod_id=module.mod_id AND module.mod_id=tree.mod_id;
	 */

	private $modules=array();
	
	private $current_module;
	
	private $current_controller;

	/**
	 *
	 * Recupère les modules accessibles du BO sous la forme d'un pseudo singleton
	 * Pour éviter les appels à la BD
	 */
	public function __construct(){
		if(empty($_SESSION['modules'])){
			Doo::loadModelAt("GlboAccessRight", "globalbo");
			$accessRight = new GlboAccessRight();
			$accessRight->usr_id = $_SESSION['id'];
			$modulesToLoad = Doo::db()->find($accessRight);
			foreach($modulesToLoad as $module){
				$tab = array();
				Doo::loadModelAt("GlboModule", "globalbo");
				$module_table = new GlboModule();
				$module_table->mod_id = $module->mod_id;
				$module_tuple = Doo::db()->find($module_table, array('limit'=> 1));
				$module_name = $module_tuple->mod_module;
				$this->modules[$module_name] = array($module_name.'_MainController' => explode(":", $module->acs_name));
				
			}
			//$this->modules['frbMessage'] = array('frbMessage_MainController' => array('index', 'readMessage'));
			//$this->modules = $this->test();
			$_SESSION['modules'] = $this->modules;
		}else{
			$this->modules = $_SESSION['modules'];
		}

	}

	/**
	 *
	 * Renvoie le nom des modules
	 * @return Array tableau qui contient les noms des modules;
	 */
	public function getModules(){
		$name = Array();
		foreach ($this->modules as $module => $a_controller ) {
			if($module!='globalbo'){
				$name[] = $module;
			}
		}
		return $name;
	}

	/**
	 *
	 * Vérifie si le module, controleur, méthode sont bien chargées
	 * @param String $search_module le nom du module à rechercher
	 * @param String $search_controller le nom du controleur à rechercher
	 * @param String $search_method le nom de la méthode à rechercher
	 * @return TRUE si les 3 paramètres représente bien un module, un controleur et une méthode chargée FALSE sinon
	 */
	public function isLoad($search_module, $search_controller, $search_method){	
		foreach ($this->modules as $module => $a_controller ) {
			if($module == $search_module){
				//echo 'module == OK';
				foreach ($a_controller as $controller => $a_method) {
					if($controller == $search_controller)
					//echo 'controller == OK';
					foreach ($a_method as $method) {
						if($method == $search_method){
							//echo 'method == OK';
							return true;
						}
					}
				}
			}
		}
		return false;
	}
	
	public function test(){
	/*$r = Doo::db()->query('select mod_module as module, mtr_controller as controller, mtr_method as method from glbo_user as user, glbo_access_right as access, glbo_module as module, glbo_module_tree as tree where user.usr_id=access.usr_id AND access.mod_id=module.mod_id AND module.mod_id=tree.mod_id;');
		$r->execute();
		$a = $r->fetchAll();
		
		foreach ($a as $value) {
			//On est en présence d'un nouveau module, on ajoute tout le tuple
			if ($value['module']!=$this->current_module){
				$result[$value['module']] = array($value['controller'] => array($value['method']));
				$this->current_module = $value['module'];
			}else{
				if($value['controller']!=$this->current_controller){
					$result[$value['module']][$value['controller']] = array($value['method']);
				}else{
					$result[$value['module']][$value['controller']][] = $value['method'];
				}
			}
		}
		return $result;
		*/
		//return $tab['test'] = array('MainController' => array('index'));
	}

}